<% title_tag t("common.login") %>

<%
auth_session = session[:omniauth] || {}
auth_info = auth_session["info"]
%>

<div class="row">
  <div class="col"></div>
  <div class="col-md-5">
    <div class="card card-l6">
      <div class="card-body">
        <div class="card-title"><%= t("common.login") %> <%= Setting.app_name %></div>

        <% if auth_info.present? %>
        <div class="alert">
          登录 <%= Setting.app_name %> 邮箱账号，以绑定 <strong><%= Homeland::Utils.omniauth_name(auth_session["provider"]) %></strong> 三方登录。
        </div>
        <% end %>

        <%= form_for resource, as: resource_name, url: session_path(resource_name), remote: true do |f| %>
          <%= render "/shared/error_messages", target: resource %>

          <div class="form-group">
            <%= f.text_field :login, class: "form-control form-control-lg", placeholder: "用户名 / Email" %>
          </div>
          <div class="form-group">
            <%= f.text_field :password, type: :password, class: "form-control form-control-lg", placeholder: "密码" %>
          </div>

          <% if devise_mapping.rememberable? -%>
          <div class="form-group">
            <div class="custom-control custom-checkbox">
              <%= f.check_box :remember_me, class: "custom-control-input" %>
              <label class="custom-control-label" for="user_remember_me"><%= t("common.remember_me") %></label>
            </div>
          </div>
          <% end -%>

          <div class="form-actions">
            <%= f.submit t("common.login"), class: "btn btn-primary btn-lg btn-block", 'data-disable-with' => t("common.logining") %>
          </div>
        <% end %>
      </div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card">
      <div class="card-header"><%= t("common.auth_with_other_services") %></div>
      <ul class="list-group list-group-flush">
        <% User.omniauth_providers.each do |provider| %>
        <li class="list-group-item omniauth-<%= provider %>">
          <%= link_to raw("<i class='fab fa-#{provider}'></i> #{Homeland::Utils.omniauth_name(provider)}"), omniauth_authorize_path(:user, provider), class: "btn btn-default btn-lg btn-block" %>
        </li>
        <% end %>
      </ul>
    </div>

    <div class="card">
      <%= render partial: "devise/shared/links" %>
    </div>
  </div>
  <div class="col"></div>
</div>

<script>
  $('#new_user').on('ajax:error', function(event, xhr, status, error) {
    var lastIndex = event.detail.length - 1
    var item = event.detail[lastIndex]

    if (item) {
      App.alert(item.responseText);
    } else {
      App.alert("未知错误");
    }

  })
</script>
